#!/usr/bin/env python
# -*- python -*-
# -*- coding: utf-8 -*-
#
#  @file rtm-skelwrapper
#  @brief CORBA skelton/stub wrapper generator
#  @date $Date: 2007-01-11 08:58:25 $
#  @author Noriaki Ando <n-ando@aist.go.jp>
#
#  Copyright (C) 2004-2007
#      Task-intelligence Research Group,
#      Intelligent Systems Research Institute,
#      National Institute of
#          Advanced Industrial Science and Technology (AIST), Japan
#      All rights reserved.
#
#  $Id$
#

from __future__ import print_function

import os
import sys
import getopt
import subprocess

if os.name != "nt":
    pyhelper_path = "@CMAKE_INSTALL_PREFIX@/@INSTALL_RTM_SCRIPT_DIR@py_helper"
    sys.path.append(pyhelper_path)


opt_args_fmt = ["help",
                "idl-file=",
                "include-dir=",
                "output-dir=",
                "skel-suffix=",
                "stub-suffix=",
                "category=",
                "dependencies=",
                "verbose"]

import skel_wrapper

config_inc = """
#include <rtm/config_rtc.h>
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
"""


def usage():
    print("""
Usage: rtm-skelwrapper [OPTIONS]

Options:

    [--help or -h]                        Print this help.
    [--idl-file[=IDL_file]]               IDL file name
    [--skel-suffix[=suffix]]              Suffix of server skelton files
    [--stub-suffix[=suffix]]              Suffix of client stub files
    [--include-dir[=dir] or -I]           include prefix in #include
    [--output-dir[=dir] or -o]            output directory
    [--category[=name] or -c]             set category name
    [--dependencies[=namelist] or -d]     set dependent idls
    [--verbose or -v]                     verbose mode

Example:
$ rtm-skelwrapper --idl=file=<IDL path>/<IDL basename>.idl
                  --include-dir=<include dir>
                  --output-dir=<output dir>
                  --skel-suffix=<skel suffix>
                  --stub-suffix=<stub suffix>
                  --category=<category name>
                  --dependencies=<dependent idl list>
                  -v

In this case, the following files are generated under <output dir>.

  <IDL basename><skel suffix>.h
  <IDL basename><skel suffix>.cpp
  <IDL basename><stub suffix>.h
  <IDL basename><stub suffix>.cpp

And these files include the target IDL file by the following
#include directive.

#include <<include dir>/<IDL basename>(CORBA impl specific suffix)>

Absolute path is not recommended for the "--include-dir" option.
When option "--include-dir=my/idl" is specified, the generated
skeleton's header includes actual CORBA implementation dependent
skeletons as follows.

#if   defined ORB_IS_TAO
#  include "my/idl/RangerC.h"
#  include "my/idl/RangerS.h"
#elif defined ORB_IS_OMNIORB
#  include "my/idl/Ranger.hh"
#endif
         
Therefore, if you compile this skeletons/stubs, you have to specify an
appropriate include directory in the compiler options.

<IDL path> is used for only include-guard. For example, if
"--idl-file=/usr/include/idl/MyInterface.idl" is specified, the
following include guard will be defined.

#ifndef _USR_INCLUDE_IDL_MYINTERFACE_H
#define _USR_INCLUDE_IDL_MYINTERFACE_H

  : (codes)

#endif // _USR_INCLUDE_IDL_MYINTERFACE_H

""")


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "f:I:o:c:hv",
                                   opt_args_fmt)
    except getopt.GetoptError:
        print("Error: Invalid options.", getopt.GetoptError, file=sys.stderr)
        usage()
        sys.exit(-1)

    if not opts:
        usage()
        sys.exit(-1)

    include_dir = ""
    output_dir = ""
    skel_suffix = "Skel"
    stub_suffix = "Stub"
    category = "OpenRTM"
    dependencies = ""
    idl_file = None
    verbose = False

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()

        if opt in ("-f", "--idl-file"):
            idl_file = arg

        if opt in ("-I", "--include-dir"):
            include_dir = arg

        if opt in ("-o", "--output-dir"):
            output_dir = arg

        if opt in ("-c", "--category"):
            category = arg

        if opt in ("-d", "--dependencies"):
            dependencies = arg

        if opt in ("--skel-suffix"):
            skel_suffix = arg

        if opt in ("--stub-suffix"):
            stub_suffix = arg

        if opt in ("-v", "--verbose"):
            verbose = True

    if not idl_file:
        print("Error: IDL file is not given.", file=sys.stderr)
        usage()
        sys.exit(-1)

    import skel_wrapper

    s = skel_wrapper.skel_wrapper(idl_file, skel_suffix, stub_suffix,
                                  include_dir, config_inc, output_dir,
                                  category, dependencies, verbose)
    s.generate()


if __name__ == "__main__":
    main()
